﻿using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace Infrastructure.Tools
{
    internal class Memento<T>
    {
        private readonly Dictionary<PropertyInfo, object> storedProperties =
            new Dictionary<PropertyInfo, object>();

        public Memento(T originator)
        {
            var propertyInfos =
                typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance)
                    .Where(p => p.CanRead && p.CanWrite);

            foreach (var property in propertyInfos)
            {
                if (property.Name == "Target")
                    continue;
                this.storedProperties.Add(property, property.GetValue(originator, null));
            }
        }

        public void Restore(T originator)
        {
            foreach (var pair in this.storedProperties)
            {
                pair.Key.SetValue(originator, pair.Value, null);
            }
        }
    }
}